
// 函数防抖
export function debounce(func, delay = 1000) { // 定时器
    let timer = null; //记录定时器
    return function () {
        if (timer) clearTimeout(timer); //清除上一次的定时器
        let _this = this;
        let args = [].slice.apply(arguments);
        timer = setTimeout(function () {
            func.apply(_this, args);
        }, delay);
    };
}

function debounce2(func, delay = 1000) {  // 时间戳
    let timer = null;
    return function () {
        let _this = this;
        let args = [].slice.apply(arguments);
        if (timer) clearTimeout(timer);
        let callNow = !timer; //类型转换
        timer = setTimeout(function () { //增加一个定时器
            timer = null; //清空当前定时器句柄
        }, delay);
        if (callNow) func.apply(_this, args); //第一次执行
    }
}

// 函数节流
function throttle1(func, wait = 1000) {  // 定时器
    let timer = null;
    return function () {
        let _this = this;
        let args = [].slice.apply(arguments);
        if (!timer) {
            timer = setTimeout(function () {
                func.apply(_this, args);
                timer = null;  //清空定时器
            }, wait);
        }

    }
}
export function throttle(func, wait) {  // 时间戳
    let prev = Date.now(); //第一次时间戳
    return function () {
        let _this = this;
        let args = [].slice.apply(arguments);
        let now = Date.now();
        if (now - prev > wait) {
            func.apply(_this, args);
            prev = now;
        }
    }
}
export function getProvider() {
    // alert(navigator.userAgent)
    // 抖音 BytedanceWebview 快手 KsWebView
    const providerType = [
        {
            key: 'Byte',
            value: 'bytedance'
        },
        {
            key: 'BytedanceWebview',
            value: 'bytedance'
        },
        {
            key: 'ByteLocale',
            value: 'bytedance'
        },
        {
            key: 'KsWebView',
            value: 'kuaishou'
        },
        {
            key: 'Kwai',
            value: 'kuaishou'
        }
    ]
    let provider = ''
    providerType.map(item => {
        if (navigator.userAgent.indexOf(item.key) > 0) provider = item.value
    })
    return provider || 'bytedance'
}
// 粉丝数量转化为K函数
export function transFans(num) {
    const val = Number(num)
    if (val >= 1000) {
        return parseFloat((val / 1000).toFixed(2)) + "K";
    } else {
        return val;
    }
}
// 手机号中间加密
export function transPhone(phone) {
    const tel = String(phone)
    return tel.substr(0,3) + "****" + tel.substr(7)
}
// 获取Url内携带的参数
export function getUrlParams(url, key){
	// \w+ 表示匹配至少一个(数字、字母及下划线), [\u4e00-\u9fa5]+ 表示匹配至少一个中文字符
	let pattern = /(\w+|[\u4e00-\u9fa5]+)=(\w+|[\u4e00-\u9fa5]+)/ig;
	let result = {};
	url.replace(pattern, ($, $1, $2)=>{
		result[$1] = $2;
	})
	return result[key]
}